const Actions = require('./actions');
const DB = require('app/db');
import { monStorage, Storage } from 'clientConfig/util/StoreData';
import urllist from 'clientConfig/urllist';
import { Control } from 'react-keeper'
const net = require('clientConfig/util/net');
const { Toast } = SaltUI;
const ui = require('clientConfig/util/jsapi/ui');
import { updateSession } from 'clientConfig/util/queryurlfield'
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        loaded: false,
        params: '',
        cb: ''
    },
    onSubmit: function (params = this.params, cb = this.cb) {
        this.params = params;
        this.cb = cb;
        let session = monStorage.get('user.session');
        let url = urllist.setUserInfo + '?session=' + session;
        let user = {}
        let modifyData = Storage.get('user.info');
        let userdata = modifyData.user;
        let is_modify = false;  
        for (let item of params) {
            if (item.modify) {
                is_modify = true;
                user[item.key] = item.content;
                userdata[item.key] = item.content;
                modifyData.user[item.key] = item.content;
            }
        }
        if (is_modify) {
            net.fetchUrl(url, { method: 'post', body: { user } }).then((jsondata) => {
                if (jsondata.errcode == 0) {
                    Storage.set('user.info', modifyData);
                    Toast.show({
                        type: 'success',
                        content: '修改资料成功'
                    });
                    //  Control.go('/home', modifyData)
                    return true;
                } else if (jsondata.errcode == 4097) {
                    updateSession(this.onSubmit);
                } else if (jsondata.errcode == 4098) {
                    // Toast.show({
                    //     type: 'error',
                    //     content: '会话id超时'
                    // });
                    updateSession(this.onSubmit);
                }
            }).then((judge) => {
                if (judge) {
                    let url = urllist.is_bind + '?id=' + Storage.get('user.id');
                    net.fetchUrl(url).then((jsondata) => {
                        let result = modifyData;
                        if (jsondata.errcode == 0) {
                            result.role = { text: jsondata.bind_info };
                            Control.go('/home', { result })
                        } else {
                            Control.go('/bind/userbind', result);
                        }
                    });
                }
            });;
        } else {
            let url = urllist.is_bind + '?id=' + Storage.get('user.id');
            net.fetchUrl(url).then((jsondata) => {
                let result = modifyData;
                if (jsondata.errcode == 0) {
                    result.role = { text: jsondata.bind_info };
                    Control.go('/home', { result })
                } else {
                    Control.go('/bind/userbind', result);
                }
            });
        }

    },

    updateComponent: function () {
        this.trigger(this.data);
    },

    getInitialState: function () {
        return this.data;
    }
});
